<template>
  <div class="page-container">
    <!-- 顶部标题栏 -->
    <header class="details-header">
      <el-page-header content="告警派送" @back="$router.back()" />
    </header>
    <main class="details-main">
      <!-- 表单容器1 -->
      <div class="details-container">
        <!-- 标题 -->
        <div class="title">告警信息：</div>
        <div class="details">
          <!-- 车辆信息列表 -->
          <el-descriptions :column="2" size="medium" :label-style="{ color: '#333', fontWeight: 'bold' }">
            <el-descriptions-item label="告警时间">{{ warningDetails.warningTime }}</el-descriptions-item>
            <el-descriptions-item label="一体杆名称">{{ warningDetails.poleName }}</el-descriptions-item>
            <el-descriptions-item label="告警区域">{{ warningDetails.areaName }}</el-descriptions-item>
            <el-descriptions-item label="一体杆编号">{{ warningDetails.poleNumber }}</el-descriptions-item>
            <el-descriptions-item label="故障类型">{{ warningDetails.errorType }}</el-descriptions-item>
            <el-descriptions-item v-if="mode === 'details'" label="处置状态">{{ handleStatusEnum[warningDetails.handleStatus] }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <!-- 表单容器2 -->
      <div class="details-container">
        <!-- 标题 -->
        <div class="title">告警处理：</div>
        <div class="details">
          <!-- 车辆信息列表 -->
          <el-descriptions v-if="mode === 'details'" :column="2" size="medium" :label-style="{ color: '#333', fontWeight: 'bold' }">
            <el-descriptions-item label="处理人">{{ warningDetails.handleUser }}</el-descriptions-item>
            <el-descriptions-item label="联系方式">{{ warningDetails.phonenumber }}</el-descriptions-item>
            <el-descriptions-item label="处理结果">{{ warningDetails.handleResult }}</el-descriptions-item>
            <el-descriptions-item label="完成时间">{{ warningDetails.handleTime }}</el-descriptions-item>
          </el-descriptions>
          <!-- 处理人分配表单 -->
          <el-form v-else :model="handleUserForm" :rules="handleUserRules" label-width="120px" size="medium">
            <el-form-item label="选择处理人:" prop="userId">
              <el-select v-model="handleUserForm.userId" placeholder="请选择处理人">
                <el-option v-for="item in userList" :key="item.userId" :label="item.userName" :value="item.userId" />
              </el-select>
            </el-form-item>
            <el-form-item label="联系方式:">
              <el-input v-model="handleUserForm.phonenumber" disabled />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onHandleWarning">派单</el-button>
              <el-button @click="$router.back()">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { getWarningDetailsAPI, handleWarningAPI } from '@/api/rod/warning'
import { getUserListAPI, getUserDetailsAPI } from '@/api/user'
export default {
  name: 'WarningDetails',
  data() {
    return {
      // 车辆信息表单数据
      warningDetails: {},
      // 处置状态枚举
      handleStatusEnum: {
        0: '未派单',
        1: '已派单',
        2: '已接单',
        3: '已完成'
      },
      userList: [],
      handleUserForm: {
        userId: '',
        phonenumber: ''
      },
      handleUserRules: {
        userId: [{ required: true, message: '请选择处理人', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 告警记录id
    warningId() {
      return this.$route.query.id
    },
    // 派单模式还是详情模式
    mode() {
      return this.$route.query.mode
    }
  },
  watch: {
    // 监听处理人id变化
    'handleUserForm.userId'(val) {
      if (val) {
        // 获取处理人详情
        this.getUserDetails()
      }
    }
  },
  async created() {
    this.getWarningDetails()
    if (this.mode === 'handle') {
      this.getUserList()
    }
  },
  methods: {
    // 获取告警详情
    async getWarningDetails() {
      this.warningDetails = await getWarningDetailsAPI(this.warningId)
    },
    // 获取用户下拉列表
    async getUserList() {
      this.userList = await getUserListAPI()
    },
    // 获取处理人详情
    async getUserDetails() {
      const res = await getUserDetailsAPI(this.handleUserForm.userId)
      this.handleUserForm.phonenumber = res.phonenumber
    },
    async onHandleWarning() {
      const data = {
        id: this.warningId,
        userId: this.handleUserForm.userId
      }
      await handleWarningAPI(data)
      this.$message.success('处理成功')
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="scss">
.page-container {
  .details-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    margin-bottom: 30px;
    height: 64px;
    background-color: #fff;
    border-bottom: 1px solid #eee;

    ::v-deep .el-page-header {
      .el-page-header__title {
        font-size: 16px;
        font-weight: 600;
        color: #444;
      }

      .el-page-header__content {
        font-size: 16px;
        font-weight: 600;
        color: #444;
      }
    }
  }

  .details-main {
    padding-left: 50px;

    .details-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        font-weight: 600;
        color: #444;
      }

      .details {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form {
          .el-input, .el-select {
            width: 300px;
          }
        }
      }
    }
  }
}
</style>
